package NWish.properties.custom
{
	import NWish.Logger;
	import NWish.json.Json;
	import NWish.properties.Properties;
	
	import away3d.containers.Scene3D;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileReference;
	import flash.net.FileFilter;
	
	import NWish.Interface.ILogger;
	
	
	public class CustomPropertiesFile extends CustomProperties
	{
		private var _file:FileReference;
		private var _parent;
		public function CustomPropertiesFile(parent)
		{
			_parent = parent;
		}
		
		//
		override public function toString()
		{
			return 'Custom File';
		}
		
		//
		override public function getJSON(obj = null,scene = null):String
		{
			_file = new FileReference();
			_file.addEventListener(Event.COMPLETE, completeHandler);
			_file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_file.addEventListener(Event.SELECT, selectHandler);
			_file.browse([new FileFilter("Json Files (*.json, *.txt, *.rtf)", "*.json;*.txt;*.rtf")]);
			return "";
		}
		
		private function completeHandler(event:Event):void {
			//ILogger.log("completeHandler: " + event);
			_parent.setText(_file.data);
			_file = null;
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void {
			ILogger.log("ioErrorHandler: " + event);
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			ILogger.log("securityErrorHandler: " + event);
		}
		
		private function selectHandler(event:Event):void {
			ILogger.log("open json file : " + _file.name);
			_file.load();
		}
	}

}